package com.meme.editor.gui.sprite2d;


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JComponent;
import java.awt.Container;
import java.io.File;

import javax.swing.JScrollPane;
import javax.swing.JSplitPane;


import com.meme.editor.gui.PropertyEditPanel;
import com.meme.editor.gui.FileEditor;

import org.apache.log4j.Logger;

import com.meme.editor.model.sprite2d.Sprite2dFile;
import com.meme.io.editor.Sprite2dXmlFileWriter;
import com.meme.util.FileUtil;
import com.meme.util.GuiUtil;
import com.meme.io.editor.FileCache;

//import java.awt.CardLayout;

public class Sprite2dMainPanel extends JPanel implements FileEditor
{
	static Logger logger = Logger.getLogger (Sprite2dMainPanel.class);
	
	JSplitPane mSplitPane;
	Sprite2dTreePanel mTreePanel;
	JComponent   mCurrentContentPanel;
	JComponent   mVoidContentPanel = new JButton ("Hello !");
	
	PropertyEditPanel mTreeNodePropertyPanel;
	
	Sprite2dFile mSprite2dFile;
	
	public Sprite2dMainPanel (Sprite2dFile sprite2dFile)
	{
		mSprite2dFile = sprite2dFile;
		mTreePanel = new Sprite2dTreePanel (sprite2dFile); 
        mCurrentContentPanel = mVoidContentPanel;
        


        
        mSplitPane = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, 
        		                               mTreePanel, 
        		                               mCurrentContentPanel );
        
        mSplitPane.setPreferredSize ( new Dimension (680, 500) );
        mSplitPane.setDividerLocation (200);
        mSplitPane.setDividerSize  (5);
        mSplitPane.setResizeWeight (0.0);
        
        setLayout ( new BorderLayout() );
        add (mSplitPane, BorderLayout.CENTER); 
	}
	
	public Sprite2dFile getSprite2dFile ()
	{
		return mSprite2dFile;
	}
	
	public static Sprite2dMainPanel getMainPanel (JComponent child)
	{
		Container  parent = child;
		while ( parent != null )
		{			
			if ( parent instanceof Sprite2dMainPanel )
				return (Sprite2dMainPanel) parent;

			parent = parent.getParent();
		}
		
		return null;
	}

	
	public void changeContentPanel (
			// EnrityObject dataSource
			JComponent newPanel)
	{
		mSplitPane.remove (mSplitPane.getRightComponent());
		if ( newPanel != null )
			mSplitPane.setRightComponent (newPanel);
		else
			mSplitPane.setRightComponent (mVoidContentPanel);
		
		mSplitPane.updateUI();
	}
	
	@Override
	public String getTitle ()
	{
		return mSprite2dFile.getName();
	}
	
	@Override
	public boolean isFileModified ()
	{
		return mSprite2dFile.isModified();
	}
	
	@Override
	public boolean isFileNew ()
	{
		return mSprite2dFile.getHostFilename() == null;
		      //||
		      //! FileUtil.getFile (mSprite2dFile.getHostFilename()).exists();
	}
	
	@Override
	public boolean onSaveFile (String filename)
	{
		if (filename == null)
		{
			logger.assertLog( ! isFileNew (), "What is the filename?!");
			
			filename = mSprite2dFile.getHostFilename();
		}
		
		//new Sprite2dXmlFileWriter ().writeToFile (mSprite2dFile, mSprite2dFile.getHostFilename());
		new Sprite2dXmlFileWriter ().writeToFile (mSprite2dFile, filename);
		mSprite2dFile.setModified(false);
		
		FileCache.registerLoadedFileAsset (mSprite2dFile.getHostFilename(), mSprite2dFile);
		
		return true;
	}
	
	@Override
	public Component getComponent ()
	{
		return this;
	}
}
